class DissolutionPanel extends BasePanel {

    public group: eui.Group;
    public group1: eui.Group;
    public group2: eui.Group;
    public group3: eui.Group;
    public group4: eui.Group;
    public img_sign1: eui.Image;
    public img_sign2: eui.Image;
    public img_sign3: eui.Image;
    public img_sign4: eui.Image;
    public lab_nick1: eui.Label;
    public lab_nick2: eui.Label;
    public lab_nick3: eui.Label;
    public lab_nick4: eui.Label;
    public lab_description: eui.Label;
    public btn_cancel: ButtonEx;
    public btn_confirm: ButtonEx;

    public constructor() {
        super();

        this.skinName = "DissolutionPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();

        this.btn_confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_cancel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_cancel:
                gameManager.socketManager.send(14, {answer: 0});  //发起解散房子
                break;
            case this.btn_confirm:
                gameManager.socketManager.send(14, {answer: 1});  //发起解散房子
                break;
        }
    }

    refresh(): void {
        if (game.dissolution && game.dissolution.vote && game.dissolution.vote.hasOwnProperty(game.player.uid)) {
            this.skinState = "after";
        }
        else {
            this.skinState = "before";
        }

        this.btn_cancel.visible = true;
        this.btn_confirm.visible = true;
        this.lab_description.visible = true;

        this.group.removeChildren();

        let index: number = 1;

        let num: number = 0;

        var refuseUID: string;

        for (let uid in game.roomPlayers) {
            let playerVo: PlayerVo = game.roomPlayers[uid];

            let lab: eui.Label = this["lab_nick" + index];

            let arrt: any[] = [];

            arrt.push({
                text: playerVo.nick,
                style: {"textColor": 0xA07A4B, "fontFamily": "Microsoft YaHei", "size": 20}
            });

            if (playerVo.status == "offline") {
                arrt.push({text: "（离线）", style: {"textColor": 0xE7432A, "fontFamily": "Microsoft YaHei", "size": 18}});
            }
            else if (playerVo.status == "online") {
                arrt.push({text: "（在线）", style: {"textColor": 0x4BA05F, "fontFamily": "Microsoft YaHei", "size": 18}});
            }
            else if (playerVo.status == "leave") {
                arrt.push({text: "（离开）", style: {"textColor": 0x7B7978, "fontFamily": "Microsoft YaHei", "size": 18}});
            }

            lab.textFlow = arrt;

            let img: eui.Image = this["img_sign" + index];
            img.visible = false;

            this.group.addChild(this["group" + index]);

            index++;

            if (game.dissolution && game.dissolution.vote) {
                if (game.dissolution.vote[uid] >= 0) {
                    img.visible = true;

                    let n: number = +game.dissolution.vote[uid];

                    if (n == 1) {
                        img.source = "diss_dui";
                    }
                    else {
                        img.source = "diss_cuo";
                        refuseUID = uid;
                        continue;
                    }
                    num++;
                }
                else {
                    img.visible = false;
                }
            }
        }

        var thiz = this;
        if (refuseUID) {
            egret.setTimeout(function () {
                EffectTextUtils.showTips(game.roomPlayers[refuseUID].nick + " 不同意解散房间", 5);
                game.dissolution = null;
                thiz.hide();
            }, this, 1000);
        }
        else {
            if (num >= 4) {
                egret.setTimeout(function () {
                    EffectTextUtils.showTips("房间解散成功", 5);
                    thiz.hide();
                }, this, 1000);
            }
        }
    }

    //回放的刷新
    replayRefresh(pList: any) {
        this.btn_cancel.visible = false;
        this.btn_confirm.visible = false;
        this.lab_description.visible = false;

        for (let i = 1; i <= 4; i++) {
            this["img_sign" + i].visible = false;
        }

        this.group.removeChildren();

        let p;
        for (let i = 1; i <= 4; i++) {
            p = pList[i];
            if (p) {
                let arrt: Array<any> = [];
                arrt.push({text: p.nick, style: {"textColor": 0xA07A4B, "fontFamily": "Microsoft YaHei", "size": 20}});

                let label: eui.Label = this["lab_nick" + p.pos];
                label.textFlow = <Array < egret.ITextElement >>arrt;

                this.group.addChild(this["group" + i]);
            }
        }
    }

    replayAgree(obj: any) {

        let img = this["img_sign" + obj.pos];

        img.visible = true;

        img.source = obj.agree ? "diss_dui" : "diss_cuo";

    }

    show() {
        super.show();

        this.refresh();
    }
}